// 引入创建store的函数
import {createStore} from 'redux'

// 准备初始数据
const initState = {
  age: 18,
  name: ''
}

// Action的类型
const actionType = {
  OLD: 'OLD',
  BIRTH: 'BIRTH',
  SETNAME: 'SETNAME'
}

// Action的生成器
export const actions = {
  old: ()=>({type: actionType.OLD}),
  birth: ()=>({type: actionType.BIRTH}),
  setName: (name)=>({type: actionType.SETNAME, payload: name})
}
// 准备reducer，用来修改数据
// 两个参数：state和action
const reducer = (state=initState, action)=> {
  switch (action.type) {
    case actionType.OLD:
      return {
        // State 是只读的,不能修改state，直接返回新的state。只换不修
        ...state,
        age: state.age + 1
      }
  
    case actionType.BIRTH:
      return {
        ...state,
        age: 0
      }
  
    case actionType.SETNAME:
      return {
        ...state,
        name: action.payload
      }
  
    default:
      return state
  }
}

// 生成store
const store = createStore(reducer);
export default store